/* vim: set ts=2 et sw=2 cindent fo=qroca: */
package com.globant.google.mendoza;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;

/**
 * This class sets up the parameters from a properties file.
 */
public final class MendozaParameters {

  /** The class logger.
   */
  private static Log log = LogFactory.getLog(MendozaParameters.class);

  /**
   * These are all the mandatory properties that should be found in the file.
   */
  private static final String [] MANDATORIES = new String [] {
      "mendoza.merchant.id",
      "mendoza.merchant.key",
      "mendoza.selenium.host",
      "mendoza.selenium.port",
      "mendoza.firefox.path",
      "mendoza.selenium.started",
      "mendoza.base.url",
      "mendoza.server.port",
      "mendoza.merchant.port",
      "mendoza.callback.url"
  };

  /**
   * The properties map that contains all the parameters and its values.
   */
  private Properties parameters;

  /**
   * Private constructor for utility class.
   * @param properties the {@link Properties} object used to load all
   * parameters.
   */
  private MendozaParameters(final Properties properties) {
    log.debug("Entering constructor");
    for (String mandatory : MANDATORIES) {
      if (!properties.containsKey(mandatory)) {
        log.error("Missing mandatory property: " + mandatory);
        throw new RuntimeException(
            "Mandatory config property not found on config file: " + mandatory);
      }
    }
    parameters = properties;
    log.debug("Leaving constructor");
  }

  /**
   * Generates the Parameters from a properties file.
   * @param file the file where the parameter values are.
   * @return the generated {@link MendozaParameters}.
   */
  public static MendozaParameters generateParameters(final String file) {
    log.debug("Entering generateParameters");
    try {
      Properties properties = new Properties();
      InputStream configFile;
      if (file == null || file.trim().equals("")) {
        log.trace("Custom config location not specified. "
            + "Looking for config file in classpath");
        configFile = ClassLoader.getSystemResource("mendoza.properties")
          .openStream();
        log.trace("Config file found in classpath");
      } else {
        configFile = new FileInputStream(file);
      }
      properties.load(configFile);
      log.debug("Leaving generateParameters");
      return new MendozaParameters(properties);
    } catch (IOException e) {
      log.error("Could not open config file: ", e);
      throw new RuntimeException("Could not open config file", e);
    }
  }

  /** Gets the merchant id.
   *
   * @return Returns the user entered merchant id.
   */
  public String getMerchantId() {
      return parameters.getProperty("mendoza.merchant.id");
  }

  /** Gets the merchant key.
   *
   * @return Returns the user entered merchant key.
   */
  public String getMerchantKey() {
      return parameters.getProperty("mendoza.merchant.key");
  }

  /** Gets the firefox-bin path.
   * @return Returns the firefox-bin path.
   */
  public String getFirefoxBinPath() {
    return parameters.getProperty("mendoza.firefox.path");
  }

  /**
   * Get the host where selenium server is running.
   * @return The host where selenium server is running.
   */
  public String getSeleniumServerHost() {
    return parameters.getProperty("mendoza.selenium.host");
  }

  /**
   * Get the port where selenium server is running.
   * @return The port where selenium server is running.
   */
  public int getSeleniumServerPort() {
    return Integer.parseInt(parameters.getProperty("mendoza.selenium.port"));
  }

  /**
   * Get the port where selenium is running. Defaulted to false.
   * @return The port where selenium is running.
   */
  public boolean getStartSeleniumServer() {
    return Boolean.parseBoolean(parameters.getProperty(
        "mendoza.selenium.started", "false"));
  }

  /** Gets the base GBuy url.
   *
   * This base url is used to post carts and perform order operations. For the
   * order operations, we append 'request' to the base url, and for posting
   * carts, we append 'checkout'. It defaults to
   * https://sandbox.google.com/checkout/cws/v2/Merchant/.
   *
   * @return Returns the user entered base GBuy url.
   */
  public String getBaseURL() {
    return parameters.getProperty("mendoza.base.url");
  }

  /** Gets the callback port number where Checkout sends notifications.
   * Same port that was configured in the merchant center.
   *
   * @return Returns the user entered port.
   */
  public int getMerchantPort() {
    return Integer.parseInt(parameters.getProperty("mendoza.merchant.port"));
  }

  /** Gets the port the mendoza server listens to.
   *
   * @return Returns the user entered port.
   */
  public int getMendozaPort() {
    return Integer.parseInt(parameters.getProperty("mendoza.server.port"));
  }

  /**
   * Gets the merchant application host.
   * @return the callback url.
   */
  public String getCallbackUrl() {
    return parameters.getProperty("mendoza.callback.url");
  }

  /** Gets the merchant calculations timeout.
  *
  * @return Returns the user entered calculations timeout.
  * It defaults to 3000 milliseconds.
  */
 public int getCalculationsTimeout() {
   return Integer.parseInt(parameters.getProperty(
       "mendoza.calculations.timeout", "3000"));
 }

  /** Gets the place order timeout.
   *
   * @return Returns the user entered place order timeout.
   * It defaults to 90000 milliseconds.
   */
  public int getPlaceOrderTimeout() {
    return Integer.parseInt(parameters.getProperty(
        "mendoza.placeorder.timeout", "90000"));
  }
}
